package logic
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import httpserver.HttpRequest;
	import httpserver.WebServerConnection;
	
	import mx.core.IFactory;
	
	import tools.json.JSON;
	import tools.json.JSONParseError;
	
	public class HttpProxy extends EventDispatcher implements IProxy
	{		
		//web server和p2p direct server 通信
		private var _webServerConnection:WebServerConnection = null;
		
		public function HttpProxy()
		{
			_webServerConnection = new WebServerConnection();
		}
		
		public function init():void
		{
			
		}
		
		public function deinit():void
		{
			
		}
		
		public function conntect():void
		{
			
		}
		
		public function sendRequest(target:String, param:Object, responseFunction:Function):void
		{
			sendHttpRequest(target, param, responseFunction);
		}
		
		public 	function requestFinished():Boolean
		{
			return (_webServerConnection._reqFinished);
		}
		private function sendHttpRequest(url:String, dataObj:Object, httpResponseFunction:Function):void
		{
			var jsonStr:String = null;
			if (dataObj != null)
			{
				try
				{
					jsonStr = tools.json.JSON.encode(dataObj)
				} 
				catch(error:Error) 
				{
					jsonStr = null;
				}
			}
			_webServerConnection.sendRequest(url, jsonStr, httpResponseFunction);
			_webServerConnection.addEventListener(HttpRequest.HTTP_REQUEST_ERROR, onHttpRequestErrorHandle);
		}
		
		private function onHttpRequestErrorHandle(event:Event):void
		{
			var pptControlEvent:PPTControlEvent;
			pptControlEvent = new PPTControlEvent(PPTControlManager.INTERNET_REQUEST_ERROR, null);
			dispatchEvent(pptControlEvent);
		}
	}
}